<template>
<view>
<!--声明表头-->
<view>
<cu-custom bgColor="bg-gradual-green" >
   <block slot="content">{{LoginInfo}}</block>
</cu-custom>
</view>
<form>
	
	<view class="cu-form-group">
		<view class="title">手机号</view>
		<input placeholder="请输入手机号" name="input" v-model="phone"></input>
	</view>
	<view class="cu-form-group">
		<view class="title">密 码</view>
		<input placeholder="请输入密码" name="input" v-model="password"></input>
	</view>
	<view class="cu-form-group">
		<view class="title">验证码</view>
		<input placeholder="请输入验证码" name="input" v-model="code"></input>
		<button class='cu-btn bg-green shadow' @click="SendPhoneCode()" :disabled="buttondisable">{{codemessage}}</button>
	</view>
	<view class="flex">
	      <view class="flex-twice">
			  <button class="cu-btn bg-blue shadow" style="width: 100%;" @click="Login()">点击这里登录</button>
		  </view>
		  
	
	</view>
	</form>

</view>
</template>

<script>
//对外暴露接口
 export default {
name:'message', 
   data () {
     return {
		 //验证码提示信息
		 codemessage:"点击这里获取验证码",
		 //登录表单用手机号
		 phone:"18240002122",
		 //登录表单用密码，
		 password:"123",
		 //动态绑定验证码按钮的是否被点击
		 buttondisable:false,
		 LoginInfo:"请登录",
		 //验证码
		 code:"",
		 //声明识别码
		 codeUUid:"",
		 //验证码倒计时默认时间
		 count:60,
		 //默认是非倒计时状态
		 iscountdown:false

     }
   },
   components: {

   },
   created() {
   
	 uni.getStorage({
	    key: 'id',
	    success: function (res) {
			console.log("有缓存");
	        uni.switchTab({
	        	url:"/pages/mypages/userinfo/userinfo"
	        })
	        
	    },
		fail:function(){
			console.log("无缓存正常登录");
		}
	    });
	
   },
   onLoad() {
   	uni.getStorage({
   	   key: 'id',
   	   success: function (res) {
   	       uni.switchTab({
   	       	url:"/pages/mypages/login"
   	       })
   	       
   	   },
   			fail:function(){
   				console.log("无缓存正常登录");
   			}
   	   });
   },
   methods:{
	   ChangeCodeMessage(count){
	   		   this.buttondisable = true
	   		   this.codemessage = "验证码已经发送"+count
	   		   count = count - 1
	   		   if(count == 0){
	   			   this.buttondisable = false
	   			   this.codemessage = "点击重新获取验证码"
	   		   }
	   		   
	   },
	   SendPhoneCode(){
		   //获取验证码之前需要进行格式验证 以及进行倒计时显示
		   var reg = /^(1[3|4|5|6|7|8|9])\d{9}$/;
		   if(!reg.test(this.phone)){
			   uni.showToast({
			   	title:"手机号码格式错误",
				icon:"none"
			   })
			   return 
		   }
		  
		   uni.request({
			   url:"http://localhost/stuapi/SendMes",
			   data:{
				   phone:this.phone
			   },
			   
			   method:"POST",
			   success: (res) => {
				       //uuiod保存在本地
					   this.codeUUid = res.data.data
					   //验证码发送成功提示信息
					   uni.showToast({
						  	title:"验证码发送成功",
						  	icon:"none",
							
						    })
							this.iscountdown = true
						//动态改变按钮值
						setInterval(()=>{
							if(this.iscountdown == true){
								this.buttondisable = true
								this.codemessage = "验证码已经发送"+this.count
								this.count = this.count - 1
								if(this.count == 0){
									   			   this.buttondisable = false
									   			   this.codemessage = "点击重新获取验证码"
												   this.count = 60
												   this.iscountdown = false
								}
							}
							else{
								return
							}
							
						
						},1000)
						
							
			       }
		   	
		   })
		   
	   },
	   //改变
	   
	   //提交验证
	   Login(){
		   //点击验证之后需要判断验证码是否填写
		   if(this.codeUUid==""){
			   uni.showToast({
				   title:"请先填写验证码",
	               icon:"none",
 })
			   return}
		   //获取所有信息
		   let data = {
			   phone : this.phone,
			   UUidcode :this.codeUUid,
			   code :this.code,
			   password:this.password
		   }
		   uni.request({
		   	url:"http://localhost/stuapi/StuLogin",
		   	data:data,
		   	method:"POST",
			header:{'content-type':'application/x-www-form-urlencoded'},
			  success: (res) => {
				  let code = res.data.code;
				  uni.showToast({
					  title:res.data.message,
					  icon:"none",
					   })
				  if(code == 200){
					  //登陆成功进行跳转
					  uni.setStorage({
					        key: 'id',
					        data: res.data.data
					      }) 
					  console.log("登录成功,缓存设置完毕")
					  uni.navigateTo({
					  	url:"/pages/mypages/userinfo/userinfo"
					  })
					  
						 
				  }
				  
			  }
		   })
		   
		   
	   }
	   
   }
 }
</script>


